package com.project.biz.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.biz.CmsSystemConfBiz;
import com.project.entity.CmsSystemConf;
import com.project.mapper.CmsSystemConfMapper;
import com.project.pojo.model.UpdateCmsSystemConfModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @Description: 系统配置业务层接口实现类
 * @Author: zhuchi
 * @CreateDate: 2020/12/17 16:51
 * @Version: 1.0
 */
@Slf4j
@Service
public class CmsSystemConfBizImpl extends ServiceImpl<CmsSystemConfMapper, CmsSystemConf> implements CmsSystemConfBiz {

    /**
     * 注入自身
     */
    @Lazy
    @Autowired
    private CmsSystemConfBiz systemConfBiz;

    /**
     * 获取系统配置信息
     * @return
     */
    public CmsSystemConf getSystemConf(){
        QueryWrapper<CmsSystemConf> queryWrapper = new QueryWrapper<>();
        CmsSystemConf systemConf = this.baseMapper.selectOne(queryWrapper);
        if (null == systemConf){
            UpdateCmsSystemConfModel updateCmsSystemConfModel = new UpdateCmsSystemConfModel();
            updateCmsSystemConfModel.setMinRecharge(new BigDecimal("0.50"));
            updateCmsSystemConfModel.setDistance(3000);
            updateCmsSystemConfModel.setCompensation(new BigDecimal("1.00"));
            systemConf = this.systemConfBiz.addSystemConf(updateCmsSystemConfModel, 1L);
        }
        return systemConf;
    }

    /**
     * 初始化系统配置
     * @param updateCmsSystemConfModel
     * @param userId
     * @return
     */
    public CmsSystemConf addSystemConf(UpdateCmsSystemConfModel updateCmsSystemConfModel, Long userId){
        CmsSystemConf systemConf = new CmsSystemConf();
        BeanUtils.copyProperties(updateCmsSystemConfModel, systemConf);
        systemConf.setCreateUser(userId);
        systemConf.setCreateTime(new Date());
        systemConf.setStatus(1);
        int addSystemConf = this.baseMapper.insert(systemConf);
        log.info("初始化系统配置信息执行状态:{}", addSystemConf);
        return systemConf;
    }

    /**
     * 修改系统配置信息
     * @param updateCmsSystemConfModel
     * @param userId
     * @return
     */
    public int updateCmsSystemConf(UpdateCmsSystemConfModel updateCmsSystemConfModel, Long userId){
        int result = 0;
        CmsSystemConf systemConf = this.getSystemConf();
        if (systemConf == null){
            systemConf = this.addSystemConf(updateCmsSystemConfModel, userId);
            if (systemConf != null && systemConf.getConfId() != null){
                result = 1;
            }
        }else {
            BeanUtils.copyProperties(updateCmsSystemConfModel, systemConf);
            systemConf.setUpdateTime(new Date());
            systemConf.setUpdateUser(userId);

            result = this.baseMapper.updateById(systemConf);
        }

        return result;
    }

}
